package com.house365.core.helper;

import java.io.File;

import android.content.Context;
import android.content.DialogInterface;

import com.house365.core.R;
import com.house365.core.constant.CorePreferences;
import com.house365.core.util.DeviceUtil;
import com.house365.core.util.FileUtil;
import com.house365.core.util.intent.IntentHelper;

public class InstallAppHelper {
	private Context mContext; 
	private String packageName;	//包名	如：cn.wps.moffice
	private String appName;	//应用名称	如：金山OFFICE
	private String apkUrl;	//安装文件下载地址	如：http://wdl.cache.ijinshan.com/wps/download/android/kingsoftoffice_2052/moffice_2052_wpscn.apk
	private boolean isconfirm;
	private static InstallAppHelper InstallAppHelper = null;
	public InstallAppHelper(Context context) {
		this.mContext = context;
	}
	public InstallAppHelper(Context context,String packagename,String appname,String apkurl) {
		this.mContext = context;
		this.packageName=packagename;
		this.apkUrl = apkurl;
		this.appName = appname;
	}
	public static InstallAppHelper getInstance(Context context) {
		if (InstallAppHelper == null) {
			return new InstallAppHelper(context);
		}
		return InstallAppHelper;
	}
	public static InstallAppHelper getInstance(Context context,String packagename,String appname,String apkurl) {
		if (InstallAppHelper == null) {
			return new InstallAppHelper(context,packagename,appname,apkurl);
		}
		return InstallAppHelper;
	}
	/***
	 * 打开文件
	 */
	public void openFile(String fileurl) {
		if (!PackageHelper.isInstalledApp(mContext, packageName)) {
			installApp();
		} else {
			if (!FileUtil.isExistFile(getFilePath(fileurl))) {
				downloadFileConfirm(fileurl);
			} else {
				IntentHelper.openFileByType(mContext, new File(
						getFilePath(fileurl)));
			}

		}
	}

	public String getFilePath(String fileurl) {
		String mFilepath = CorePreferences.getAppDownloadSDPath() + "/"
				+ FileUtil.getFileNameFromUrl(fileurl);
		return mFilepath;
	}

	/***
	 * 安装app应用
	 */
	private void installApp() {

		final String installapkfile = CorePreferences.getAppTmpSDPath() + "/"
				+ FileUtil.getFileNameFromUrl(apkUrl);
		
		if (!FileUtil.isExistFile(installapkfile)) {
			
		if (DeviceUtil.isWifiConnect(mContext)) {
			String downloadtitle = mContext.getResources().getString(
					R.string.confirm_install_title);
			String downloadmessage = mContext.getResources().getString(
					R.string.apk_confirm_install_msg,appName);
			final String downloading = mContext.getResources().getString(
					R.string.apk_downloading,appName);
			ViewHelper.showConfirmDialog(mContext, downloadtitle,
					downloadmessage, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Downloadhelper dm = new Downloadhelper(mContext,true);
							dm.downLoadFile(downloading, apkUrl,
									installapkfile);
						}
					});
		} else {
			ViewHelper.showDialog(mContext, R.string.confirm_wifinet_title,
					R.string.confirm_nowifi_install_msg,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});

		}
		
		}else{
			//安装文件存在直接安装
			IntentHelper.openFileByType(mContext, new File(installapkfile));
		}

	}

	/***
	 * 下载文件
	 * 
	 * @param fileurl
	 */
	private void downloadFileConfirm(final String fileurl) {

		if (!DeviceUtil.isWifiConnect(mContext)) {
			String downloadtitle = mContext.getResources().getString(
					R.string.confirm_wifinet_title);
			String downloadmessage = mContext.getResources().getString(
					R.string.confirm_wifinet_msg);
			ViewHelper.showConfirmDialog(mContext, downloadtitle,
					downloadmessage, R.string.confirm_download_button_ok,
					R.string.confirm_download_button_cancel,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							downloadFile(fileurl);
						}
					});
		} else {
			downloadFile(fileurl);
		}
	}

	/***
	 * 下载文件
	 * 
	 * @param downloading
	 */
	private void downloadFile(String fileurl) {
		String downloading = mContext.getResources().getString(
				R.string.downloading);
		Downloadhelper dm = new Downloadhelper(mContext);
		dm.downLoadFile(downloading, fileurl, getFilePath(fileurl));
	}

}
